package com.atom.wifiupload.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.atom.wifiupload.R;
import com.atom.wifiupload.bean.FileBean;
import com.atom.wifiupload.utils.FileTools;
import com.atom.wifiupload.utils.FileUtils;
import com.atom.wifiupload.utils.TimeUtils;

import java.io.File;
import java.util.List;

/**
 * ============================================================
 * project: WIFI-Upload
 * package: com.atom.wifiupload.adapter
 * fileDescribe:
 * user: admin
 * email: 1299854942@qq.com
 * createTime: 2017/6/24  13:01
 * modify:
 * version:: V1.0
 * ============================================================
 **/
public class BookAdapter extends RecyclerView.Adapter<BookAdapter.BookViewHolder> {


    private Context mContext;
    private List<FileBean> mFileBeans;

    public BookAdapter(Context mContext, List<FileBean> fileBeens) {
        this.mContext = mContext;
        this.mFileBeans = fileBeens;
    }

    @Override
    public BookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_book, parent, false);
        return new BookViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(BookViewHolder holder, int position) {
        final FileBean fileBean = mFileBeans.get(position);
        holder.mFileName.setText(fileBean.getFilename());
        holder.mFileTime.setText(TimeUtils.getSimpleTime(fileBean.getCreateTime()));
        holder.mFileSize.setText(FileUtils.formatFileSizeToString(fileBean.getFileSize()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //浏览文件
                fileEidt(fileBean.getFilepath());
            }
        });
    }

    /**
     * 文件编辑
     */
    public void fileEidt(String loalPath) {
        Log.e("fileEidt", "loalPath = " + loalPath);

        File file = new File(loalPath);

        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //设置intent的Action属性
        intent.setAction(Intent.ACTION_VIEW);
        //获取文件file的MIME类型
        String type = FileTools.getMIMEType(file);
        //设置intent的data和Type属性。
        intent.setDataAndType(/*uri*/Uri.fromFile(file), type);
        //跳转
        mContext.startActivity(intent);
    }


    @Override
    public int getItemCount() {
        return null == mFileBeans ? 0 : mFileBeans.size();
    }

    class BookViewHolder extends RecyclerView.ViewHolder {
        TextView mFileName;
        TextView mFileSize;
        TextView mFileTime;

        public BookViewHolder(View itemView) {
            super(itemView);
            mFileName = (TextView) itemView.findViewById(R.id.tv_file_name);
            mFileSize = (TextView) itemView.findViewById(R.id.tv_size);
            mFileTime = (TextView) itemView.findViewById(R.id.tv_time);

        }
    }

}
